package com.ant.storage.controller;

import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.*;
import com.ant.storage.pojo.PurchaseOrders;
import com.ant.storage.pojo.Subscribe;
import com.ant.storage.service.IPurchaseOrdersService;
import com.ant.storage.service.ISubscribeService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/subscribe")
@Api(tags = "采购单信息接口")
public class SubscribeController {

    @Autowired
    private ISubscribeService subscribeService;

    @Autowired
    private IPurchaseOrdersService purchaseOrdersService;

    @ApiOperation(value = "获取申请采购单数据列表")
    @GetMapping("/getSubscribePage")
    public ResultPage getSubscribePage(SubscribeDto subscribeDto) {
        return ResultPage.success("获取成功", subscribeService.queryAllPage(subscribeDto));
    }

    @ApiOperation(value = "小程序获取采购单")
    @GetMapping("/getAppletSubscribePage")
    public ResultPage getAppletSubscribePage(SubscribeDto subscribeDto) {
        return ResultPage.success("获取成功", subscribeService.appletQueryAllPage(subscribeDto));
    }

    @ApiOperation(value = "通过id获取采购单详情")
    @GetMapping("/getPurchaseOrderById/{id}")
    public ResultVo getPurchaseOrderById(@PathVariable("id") Integer id) {
        Subscribe purchaseOrderById = subscribeService.getPurchaseOrderById(id);
        if (purchaseOrderById != null) {
            return ResultVo.success("获取成功", purchaseOrderById);
        }
        return ResultVo.fail("暂无数据");
    }

    @ApiOperation(value = "通过id获取采购单详情")
    @GetMapping("/getAppletPurchaseOrderById/{ids}")
    public ResultVo getAppletPurchaseOrderById( @PathVariable("ids") Integer id) {
        Subscribe purchaseOrderById = subscribeService.getAppletPurchaseOrderById(id);
        if (purchaseOrderById != null) {
            return ResultVo.success("获取成功", purchaseOrderById);
        }
        return ResultVo.fail("暂无数据");
    }


    @ApiOperation(value = "通过id获取采购单详情")
    @GetMapping("/getAppletPurchaseOrderByIds/{ids}/{pid}")
    public ResultVo getAppletPurchaseOrderByIds(@PathVariable("ids") String ids,
                                                @PathVariable("pid") Integer pid) {
        List<Subscribe> purchaseOrderById = subscribeService.getAppletPurchaseOrderByIds(ids, pid);
        if (purchaseOrderById != null) {
            return ResultVo.success("获取成功", purchaseOrderById);
        }
        return ResultVo.fail("暂无数据");
    }



    @ApiOperation(value = "添加计划采购单")
    @PostMapping("/addPlanPurchase")
    public ResultVo addPlanPurchase(@Valid @RequestBody SubscribeDto subscribeDto, BindingResult bindingResult) {
        return subscribeService.addPlanPurchase(subscribeDto, bindingResult);
    }


    @ApiOperation(value = "钉钉申请通过生成采购单")
    @GetMapping("/generatePurchaseOrder/{id}/{status}")
    public ResultVo generatePurchaseOrder(@PathVariable(value = "id", required = false) Integer id,
                                          @PathVariable(value = "status", required = false) Integer status) {
        return subscribeService.generatePurchaseOrder(id, status);
    }

    @ApiOperation(value = "单个物品完成采购")
    @PostMapping("/goodSingleCompletion")
    public ResultVo goodSingleCompletion(@RequestBody PurchaserDto purchasesDto) {
        return subscribeService.goodSingleCompletion(purchasesDto);
    }

    @ApiOperation(value = "采购单完成")
    @PostMapping("/goodAllCompleted")
    public ResultVo goodAllCompleted(@RequestParam(required = false) Integer id,
                                     @RequestParam(required = false) Integer uid) {
        return subscribeService.goodAllCompleted(id,uid);
    }

    @ApiOperation(value = "小程序采购单完成采购")
    @PostMapping("/subscribeAllCompleted")
    public ResultVo subscribeAllCompleted(@RequestBody List<SubDto> dto) {
        return subscribeService.subscribeAllCompleted(dto);
    }




    @ApiOperation(value = "小程序查询采购单报销")
    @GetMapping("/subscribeReimbursement/{ids}/{purchaserId}")
    public ResultVo subscribeReimbursement(@PathVariable(value = "ids", required = false) Integer[] ids,
                                           @PathVariable(value = "purchaserId", required = false) Integer purchaserId) {
        return subscribeService.subscribeReimbursement(ids, purchaserId);
    }

    @ApiOperation(value = "取消采购单")
    @GetMapping("/cancelOrder/{id}/{status}")
    public ResultVo cancelOrder(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        subscribeService.update(new UpdateWrapper<Subscribe>().set("status", status).eq("id", id));
        // 批量取消  采购单列表
        if (status == 2) {
            purchaseOrdersService.update(new UpdateWrapper<PurchaseOrders>()
                    .set("goods_status", 4).eq("parent_id", id));
        }
        return ResultVo.success("已取消采购");
    }

    @ApiOperation(value = "采购入库")
    @PostMapping("/PurchaseWarehousing")
    public ResultVo PurchaseWarehousing(@RequestParam(required = false) Integer id) {
        return subscribeService.PurchaseWarehousing(id);

    }

    @ApiOperation(value = "采购单完成")
    @GetMapping("/price")
    public ResultVo price() {
        return subscribeService.price();
    }


    @ApiOperation(value = "修改票据")
    @PostMapping("/updateImgUrl")
    public ResultVo updateImgUrl(@RequestBody ImgUrlDto dto) {
        return subscribeService.updateImgUrl(dto);
    }


    @ApiOperation(value = "小程序采购单请款处理")
    @PostMapping("/getMergeApply")
    public ResultVo getMergeApply(@RequestBody MergeDto dto) {
        return subscribeService.getMergeApply(dto);

    }

    @ApiOperation(value = "小程序采通过采购单id查看申请申请")
    @GetMapping("/findSubscribeIdPurchaserRecord/{id}")
    public ResultVo findSubscribeIdPurchaserRecord(@PathVariable("id") Integer id) {
        return ResultVo.success("获取成功", subscribeService.findSubscribeIdPurchaserRecord(id));
    }


    @ApiOperation(value = "测试")
    @GetMapping("/test")
    public ResultVo test() {
        subscribeService.getprocessInstanceIdById("-qQg6VfOSaqgDBukb1uAkQ03271685581108","agree");
        return ResultVo.success("获取成功");
    }



    @ApiOperation(value = "获取申请采购单数据列表")
    @GetMapping("/getSubscribeWarehousingPage")
    public ResultVo getSubscribeWarehousingPage(SubscribeDto subscribeDto) {
        return ResultVo.success("获取成功", subscribeService.queryWarehousingAllPage(subscribeDto));
    }




    @ApiOperation(value = "获取申请采购单数据列表车辆类型")
    @GetMapping("/getCarSubscribePage")
    public ResultVo getCarSubscribePage(SubscribeDto dto) {
        return ResultVo.success("获取成功", subscribeService.queryCarAllPage(dto));
    }


    @ApiOperation(value = "车蚂蚁通过采购")
    @GetMapping("/getCarAdoptApply/{id}")
    public ResultVo getCarAdoptApply(@PathVariable("id")Integer id) {
        subscribeService.getCarAdoptApply(id);
        return ResultVo.success("操作成功");
    }
}
